<?php

namespace App\Http\Controllers\Admin;

use App\Models\Conditions\ArticleCondition;
use App\Services\ArticleCategoryService;
use App\Services\ArticleService;
use Illuminate\Http\Request;
use App\Http\Requests\Admin\ArticleRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{


    private $articleService;
    private $articleCategoryService;

    public function __construct(ArticleService $articleService, ArticleCategoryService $articleCategoryService)
    {
        $this->articleService = $articleService;
        $this->articleCategoryService = $articleCategoryService;
    }

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $condition = new ArticleCondition();
        $condition->title = $request->input('title');
        $condition->article_category_id = $request->input('category');
        $request->flashOnly('title', 'category');
        $articleCategories = $this->articleCategoryService->getTreeArticleCategory();
        $articles = $this->articleService->getArticlesByPageAndCondition($condition);
        return view('admin.articles.index', ['articles' => $articles, 'articleCategories' => $articleCategories]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $articleCategories = $this->articleCategoryService->getAllArticleCategories();
        return view('admin.articles.create', ['articleCategories' => $articleCategories]);

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\Admin\ArticleRequest $request
     * @return \Illuminate\Http\Response
     */
    public function store(ArticleRequest $request)
    {
        $this->articleService->saveArticle($request->all());
        return redirect()->route('admin.articles.index');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $article = $this->articleService->getArticleById($id);
        $articleCategories = $this->articleCategoryService->getAllArticleCategories();
        return view('admin.articles.edit', ['article' => $article, 'articleCategories' => $articleCategories]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\Admin\ArticleRequest $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(ArticleRequest $request, $id)
    {
        $this->articleService->updateArticleById($id, $request->all());
        return redirect()->route('admin.articles.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->articleService->deleteArticleById($id);
        return redirect()->route('admin.articles.index');
    }

    /**
     * 删除多篇文章
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroys(Request $request)
    {
        $ids = explode(',', $request->input('ids'));
        $this->articleService->deleteArticleByIds($ids);
        return redirect()->route('admin.articles.index');
    }

}
